home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / rmtlog1a / nteventl.cls < prev    next >
Text File  |  1999-03-21  |  5KB  |  221 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4.   Persistable = 0  'NotPersistable
  5.   DataBindingBehavior = 0  'vbNone
  6.   DataSourceBehavior  = 0  'vbNone
  7.   MTSTransactionMode  = 0  'NotAnMTSObject
  8. END
  9. Attribute VB_Name = "NTEventLog"
  10. Attribute VB_GlobalNameSpace = False
  11. Attribute VB_Creatable = True
  12. Attribute VB_PredeclaredId = False
  13. Attribute VB_Exposed = True
  14. Attribute VB_Ext_KEY = "SavedWithClassBuilder6" ,"Yes"
  15. Attribute VB_Ext_KEY = "Top_Level" ,"Yes"
  16. Option Explicit
  17.  
  18. Implements EventLog
  19.  
  20. Private mobjLog As New EventLog
  21. Private mstrSource As String
  22. Private mstrUNCHost As String
  23. Private hEventLog As Long
  24. Private mbEnabled As Boolean
  25.  
  26. ' Required Interface Elements - EventLog
  27. Private Property Let EventLog_Enabled(ByVal RHS As Boolean)
  28.  
  29.   Enabled = RHS
  30.  
  31. End Property
  32.  
  33. Private Property Get EventLog_Enabled() As Boolean
  34.  
  35.   EventLog_Enabled = Enabled
  36.  
  37. End Property
  38.  
  39. Private Property Let EventLog_EventSource(ByVal RHS As String)
  40.  
  41.   EventSource = RHS
  42.  
  43. End Property
  44.  
  45. Private Property Get EventLog_EventSource() As String
  46.  
  47.   EventLog_EventSource = EventSource
  48.  
  49. End Property
  50.  
  51. Private Property Let EventLog_LogDestination(ByVal RHS As String)
  52.   
  53.   LogDestination = RHS
  54.  
  55. End Property
  56.  
  57. Private Property Get EventLog_LogDestination() As String
  58.  
  59.   EventLog_LogDestination = LogDestination
  60.  
  61. End Property
  62.  
  63. Private Function EventLog_WriteLog(Message As String, EventType As LogEventTypes, EventID As Long) As Boolean
  64.  
  65.   EventLog_WriteLog = WriteLog(Message, EventType, EventID)
  66.  
  67. End Function
  68.  
  69. Private Property Get EventLog_UserName() As String
  70.  
  71.   EventLog_UserName = UserName
  72.  
  73. End Property
  74.  
  75. Private Property Get EventLog_ComputerName() As String
  76.  
  77.   EventLog_ComputerName = ComputerName
  78.  
  79. End Property
  80.  
  81.  
  82. ' Private Member Functions - NTEventLog
  83. Private Sub DisconnectEventSource()
  84.  
  85.   If hEventLog Then
  86.     DeregisterEventSource (hEventLog)
  87.     hEventLog = 0
  88.   End If
  89.   
  90.   mbEnabled = (hEventLog <> 0)
  91.  
  92. End Sub
  93. Private Sub ConnectEventSource()
  94.  
  95.   If hEventLog Then
  96.     Err.Raise vbObjectError + ERR_CONNECT_FAILED, App.Title, "Cannot connect to Event Log - Log Handle is already allocated"
  97.     Exit Sub
  98.   End If
  99.   
  100.   On Error GoTo ConnectFailure
  101.   
  102.   hEventLog = RegisterEventSource(mstrUNCHost, mstrSource)
  103.   
  104.   mbEnabled = (hEventLog <> 0)
  105.   
  106.   Exit Sub
  107.   
  108. ConnectFailure:
  109.   Err.Raise vbObjectError + Err.Number, App.Title, Err.Description
  110.  
  111. End Sub
  112.  
  113. ' Public Interface Members - NTEventLog
  114. Public Function WriteLog(Message As String, EventType As LogEventTypes, EventID As Long) As Boolean
  115.  
  116.   Dim hMsg As Long
  117.   Dim cLen As Long
  118.   Dim iCount As Integer
  119.   Dim iType As Integer
  120.   
  121.   iType = EventType
  122.   
  123.   cLen = Len(Message) + 1
  124.   
  125.   hMsg = GlobalAlloc(GMEM_ZEROINIT, cLen)
  126.   CopyMemory ByVal hMsg, ByVal Message, cLen
  127.   iCount = 1
  128.   
  129.   If ReportEvent(hEventLog, iType, CInt(0), EventID, CLng(0), iCount, cLen, hMsg, CLng(0)) = 0 Then
  130.     WriteLog = False
  131.   Else
  132.     WriteLog = True
  133.   End If
  134.   
  135.   Call GlobalFree(hMsg)
  136.  
  137. End Function
  138.  
  139. Private Sub Class_Terminate()
  140.  
  141.   DisconnectEventSource
  142.  
  143. End Sub
  144.  
  145. Public Property Get EventSource() As String
  146.  
  147.   EventSource = mstrSource
  148.  
  149. End Property
  150.  
  151. Public Property Let EventSource(ByVal vNewValue As String)
  152.  
  153.   If mbEnabled Then
  154.     Err.Raise vbObjectError + ERR_SOURCE_DISABLED, App.Title, "Can't set EventSource property while Enabled = True"
  155.     Exit Property
  156.   End If
  157.   
  158.   If Trim(vNewValue) = "" Then
  159.     Err.Raise vbObjectError + ERR_SOURCE_NULL, App.Title, "EventSource can't be a zero-length string"
  160.     Exit Property
  161.   End If
  162.   
  163.   mstrSource = vNewValue
  164.  
  165. End Property
  166.  
  167. Public Property Get LogDestination() As String
  168.  
  169.   LogDestination = mstrUNCHost
  170.  
  171. End Property
  172.  
  173. Public Property Let LogDestination(ByVal vNewValue As String)
  174.  
  175.   If mbEnabled Then
  176.     Err.Raise vbObjectError + ERR_LOGDEST_DISABLED, App.Title, "Can't change LogDestination while Enabled property = True"
  177.     Exit Property
  178.   End If
  179.   
  180.   If Trim(vNewValue) = "" Then
  181.     Err.Raise vbObjectError + ERR_LOGDEST_NULL, App.Title, "LogDestination can't be a zero-length string"
  182.     Exit Property
  183.   End If
  184.   
  185.   mstrUNCHost = vNewValue
  186.  
  187. End Property
  188.  
  189. Public Property Get Enabled() As Boolean
  190.  
  191.   Enabled = mbEnabled
  192.  
  193. End Property
  194.  
  195. Public Property Let Enabled(ByVal NewEnabled As Boolean)
  196.  
  197.   If mbEnabled = NewEnabled Then
  198.     Exit Property
  199.   End If
  200.   
  201.   If mbEnabled Then
  202.     DisconnectEventSource
  203.   Else
  204.     ConnectEventSource
  205.   End If
  206.  
  207. End Property
  208.  
  209. Private Property Get UserName() As String
  210.  
  211.   UserName = mobjLog.UserName
  212.  
  213. End Property
  214.  
  215. Private Property Get ComputerName() As String
  216.  
  217.   ComputerName = mobjLog.ComputerName
  218.  
  219. End Property
  220.  
  221.